package com.itboye.textileexpres.top;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.textileexpes.beans.GoodsBean;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.util.TimeFormat;
import com.itboye.textileexpres.util.XImageLoader;
import com.itboye.textileexptes.config.GlobalConfig;

public class GoodsFragmentAdapter extends BaseAdapter {
	Context context;
	List<GoodsBean.GoodsList> list = new ArrayList<GoodsBean.GoodsList>();
	int type;
	private final int TYPE_1 = 0;
	private final int TYPE_2 = 1;
	private final int TYPE_3 = 2;
	OnClickListener deleteonclick;
	OnClickListener collectiononclick;

	public GoodsFragmentAdapter(Context context,
			List<GoodsBean.GoodsList> list, OnClickListener deleteonclick,
			OnClickListener collectiononclick) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.list = list;
		this.deleteonclick = deleteonclick;
		this.collectiononclick = collectiononclick;
	}

	public GoodsFragmentAdapter(Context context, int type) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.type = type;
	}

	public GoodsFragmentAdapter(Context context) {
		// TODO Auto-generated constructor stub
		this.context = context;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		if ("0".equals(list.get(position).getTemplate_id())) {
			return TYPE_1;
		} else if ("1".equals(list.get(position).getTemplate_id())) {
			return TYPE_2;
		} else if ("2".equals(list.get(position).getTemplate_id())) {
			return TYPE_3;
		} else {
			return TYPE_3;
		}
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return 3;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		type = getItemViewType(position);
		if (convertView == null) {
			holder = new ViewHolder();
			LayoutInflater inflater = LayoutInflater.from(context);
			switch (type) {
			case TYPE_1:
				convertView = inflater.inflate(R.layout.item_goods_1, parent,
						false);
				holder.publish_time = (TextView) convertView
						.findViewById(R.id.publish_time);
				holder.title = (TextView) convertView.findViewById(R.id.title);
				holder.collection_img = (ImageView) convertView
						.findViewById(R.id.collection_img);
				holder.delete_img = (ImageView) convertView
						.findViewById(R.id.delete_img);
				holder.goods_img = (ImageView) convertView
						.findViewById(R.id.goods_img);
				holder.context = (TextView) convertView
						.findViewById(R.id.context);
				holder.number = (TextView) convertView
						.findViewById(R.id.number);
				convertView.setTag(holder);
				break;
			case TYPE_2:
				convertView = inflater.inflate(R.layout.item_goods_2, parent,
						false);
				holder.publish_time = (TextView) convertView
						.findViewById(R.id.publish_time);
				holder.title = (TextView) convertView.findViewById(R.id.title);
				holder.context = (TextView) convertView
						.findViewById(R.id.context);
				holder.number = (TextView) convertView
						.findViewById(R.id.number);
				holder.goods_img = (ImageView) convertView
						.findViewById(R.id.goods_img);
				holder.collection_img = (ImageView) convertView
						.findViewById(R.id.collection_img);
				holder.delete_img = (ImageView) convertView
						.findViewById(R.id.delete_img);
				convertView.setTag(holder);
				break;
			case TYPE_3:
				convertView = inflater.inflate(R.layout.item_goods_3, parent,
						false);
				holder.publish_time = (TextView) convertView
						.findViewById(R.id.publish_time);
				holder.title = (TextView) convertView.findViewById(R.id.title);
				holder.context = (TextView) convertView
						.findViewById(R.id.context);
				holder.goods_img = (ImageView) convertView
						.findViewById(R.id.goods_img);
				holder.number = (TextView) convertView
						.findViewById(R.id.number);
				holder.collection_img = (ImageView) convertView
						.findViewById(R.id.collection_img);
				holder.delete_img = (ImageView) convertView
						.findViewById(R.id.delete_img);
				convertView.setTag(holder);
				break;
			default:
				break;
			}
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.context.setText(list.get(position).getSecondary_headlines());
		holder.delete_img.setTag(position);
		holder.title.setText(list.get(position).getName());
		holder.number.setText(list.get(position).getP_no());
		holder.publish_time.setText(TimeFormat.DateFormat(list.get(position)
				.getUpdatetime()));
		holder.collection_img.setTag(position);
		XImageLoader.load(GlobalConfig.IMAGE_DOUBLE
				+ list.get(position).getId(), holder.goods_img);
		holder.delete_img.setOnClickListener(deleteonclick);
		holder.collection_img.setOnClickListener(collectiononclick);

		return convertView;
	}

	class ViewHolder {
		TextView publish_time, title, context, number;
		ImageView collection_img, delete_img, goods_img;
	}

}
